﻿//Copyright (c) 2009, Offbeat Guides
//All rights reserved.
//
//Redistribution and use in source and binary forms, with or without modification, are 
//permitted provided that the following conditions are met:
//
//* Redistributions of source code must retain the above copyright notice, this list of 
//conditions and the following disclaimer.
//
//* Redistributions in binary form must reproduce the above copyright notice, this list 
//of conditions and the following disclaimer in the documentation and/or other materials 
//provided with the distribution.
//
//* Neither the name of Offbeat Guides nor the names of its contributors may be used to 
//endorse or promote products derived from this software without specific prior written 
//permission.
//
//THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
//EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
//OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
//SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 
//SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT 
//OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 
//HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 
//TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 
//EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using Amazon.Services;
using System.Globalization;
using System.IO;

namespace Amazon.SimpleDB
{
    public class SimpleDBConfiguration : Amazon.Services.IServiceConfiguration
    {
        #region Static Variables
        private static readonly Encoding Encoding = new UTF8Encoding();
        public static readonly SimpleDBConfiguration Default;
        #endregion

        #region IAmazonConfig Properties
        public String ServiceVersion { get; set; }

        public String SignatureMethod { get; set; }

        public String SignatureVersion { get; set; }

        public String UserAgent { get; set; }

        public String ServiceUrl { get; set; }

        public String ProxyHost { get; set; }

        public Int32 ProxyPort { get; set; }

        public Int32 MaxErrorRetry { get; set; }
        #endregion

        #region Constructors and Finalizers
        static SimpleDBConfiguration()
        {
            SimpleDBConfiguration.Default = new SimpleDBConfiguration();
            SimpleDBConfiguration.Default.ServiceVersion = "2009-04-15";
            SimpleDBConfiguration.Default.ServiceUrl = "https://sdb.amazonaws.com";
            SimpleDBConfiguration.Default.UserAgent = "Amazon Simple DB CSharp Library";
            SimpleDBConfiguration.Default.SignatureVersion = "2";
            SimpleDBConfiguration.Default.SignatureMethod = "HmacSHA256";
            SimpleDBConfiguration.Default.ProxyHost = null;
            SimpleDBConfiguration.Default.ProxyPort = -1;
            SimpleDBConfiguration.Default.MaxErrorRetry = 3;
        }
        #endregion

        #region Methods
        
        #endregion

        #region IServiceConfiguration Methods
        public WebRequest CreateRequest(AmazonAccount account, IDictionary<String, String> parameters, Byte[] contents)
        {
            // add required request parameters
            if (!parameters.ContainsKey("AWSAccessKeyId"))
                parameters.Add("AWSAccessKeyId", account.Key);
            if (!parameters.ContainsKey("Timestamp"))
                parameters.Add("Timestamp", DateTime.UtcNow.ToString("yyyy-MM-dd\\THH:mm:ss.fff\\Z", CultureInfo.InvariantCulture));
            if (!parameters.ContainsKey("Version"))
                parameters.Add("Version", this.ServiceVersion);
            if (!parameters.ContainsKey("SignatureVersion"))
                parameters.Add("SignatureVersion", this.SignatureVersion);
            if (!parameters.ContainsKey("Signature"))
                parameters.Add("Signature", Amazon.Services.OAuth.GenerateSignature(this, "POST", account.SecretKey, parameters));

            String queryString = Utility.ToQueryString(parameters);
            byte[] contentData = SimpleDBConfiguration.Encoding.GetBytes(queryString);

            // create a request to the servers
            HttpWebRequest request = WebRequest.Create(this.ServiceUrl) as HttpWebRequest;
            request.UserAgent = this.UserAgent;
            request.Method = "POST";
            request.Timeout = 50000;
            request.ContentType = "application/x-www-form-urlencoded; charset=utf-8";
            request.ContentLength = contentData.Length;

            // if a proxy host is defined, then use it for requests.
            if (!String.IsNullOrEmpty(this.ProxyHost))
                request.Proxy = new WebProxy(this.ProxyHost, this.ProxyPort);

            // write out the request to the stream
            using (Stream stream = request.GetRequestStream())
                stream.Write(contentData, 0, contentData.Length);

            return request;
        }
        #endregion
    }
}
